<html>

<head>
	<meta charset="utf-8">
	<title>VSCode Tests</title>
	<link href="../../../node_modules/mocha/mocha.css" rel="stylesheet" />
</head>

<body>
	<div id="mocha"></div>
	<script src="../../../node_modules/mocha/mocha.js"></script>
	<script>
		// !!! DO NOT CHANGE !!!
		// Our unit tests may run in environments without
		// display (e.g. from builds) and tests may by
		// accident bring up native dialogs or even open
		// windows. This we cannot allow as it may crash
		// the test run.
		// !!! DO NOT CHANGE !!!
		window.open = function () { throw new Error('window.open() is not supported in tests!'); };
		window.alert = function () { throw new Error('window.alert() is not supported in tests!'); }
		window.confirm = function () { throw new Error('window.confirm() is not supported in tests!'); }

		// Ignore uncaught cancelled promise errors
		window.addEventListener('unhandledrejection', e => {
			const name = e && e.reason && e.reason.name;

			if (name === 'Canceled') {
				e.preventDefault();
				e.stopPropagation();
			}
		});

		mocha.setup({
			ui: 'tdd',
			timeout: 5000
		});
	</script>

	<!-- Depending on --build or not, load loader from known locations -->
	<script src="../../../out/vs/loader.js"></script>
	<script src="../../../out-build/vs/loader.js"></script>

	<script>
		const urlParams = new URLSearchParams(window.location.search);
		const isBuild = urlParams.get('build');

		// configure loader
		const baseUrl = window.location.href;
		require.config({
			catchError: true,
			baseUrl: new URL('../../../src', baseUrl).href,
			paths: {
				'vs': new URL(`../../../${!!isBuild ? 'out-build' : 'out'}/vs`, baseUrl).href,
				assert: new URL('../assert.js', baseUrl).href,
				sinon: new URL('../../../node_modules/sinon/pkg/sinon-1.17.7.js', baseUrl).href,
				xterm: new URL('../../../node_modules/xterm/lib/xterm.js', baseUrl).href
			}
		});
	</script>

	<script>
		function serializeSuite(suite) {
			return {
				root: suite.root,
				suites: suite.suites.map(serializeSuite),
				tests: suite.tests.map(serializeRunnable),
				title: suite.title,
				fullTitle: suite.fullTitle(),
				timeout: suite.timeout(),
				retries: suite.retries(),
				enableTimeouts: suite.enableTimeouts(),
				slow: suite.slow(),
				bail: suite.bail()
			};
		}
		function serializeRunnable(runnable) {
			return {
				title: runnable.title,
				fullTitle: runnable.fullTitle(),
				async: runnable.async,
				slow: runnable.slow(),
				speed: runnable.speed,
				duration: runnable.duration
			};
		}
		function serializeError(err) {
			return {
				message: err.message,
				stack: err.stack,
				actual: err.actual,
				expected: err.expected,
				uncaught: err.uncaught,
				showDiff: err.showDiff,
				inspect: typeof err.inspect === 'function' ? err.inspect() : ''
			};
		}
		function PlaywrightReporter(runner) {
			runner.on('start', () => window.mocha_report('start'));
			runner.on('end', () => window.mocha_report('end'));
			runner.on('suite', suite => window.mocha_report('suite', serializeSuite(suite)));
			runner.on('suite end', suite => window.mocha_report('suite end', serializeSuite(suite)));
			runner.on('test', test => window.mocha_report('test', serializeRunnable(test)));
			runner.on('test end', test => window.mocha_report('test end', serializeRunnable(test)));
			runner.on('hook', hook => window.mocha_report('hook', serializeRunnable(hook)));
			runner.on('hook end', hook => window.mocha_report('hook end', serializeRunnable(hook)));
			runner.on('pass', test => window.mocha_report('pass', serializeRunnable(test)));
			runner.on('fail', (test, err) => window.mocha_report('fail', serializeRunnable(test), serializeError(err)));
			runner.on('pending', test => window.mocha_report('pending', serializeRunnable(test)));
		};

		window.loadAndRun = async function loadAndRun(modules, manual = false) {
			// load
			await Promise.all(modules.map(module => new Promise((resolve, reject) => {
				require([module], resolve, err => {
					console.log("BAD " + module + JSON.stringify(err, undefined, '\t'));
					// console.log(module);
					resolve({});
				});
			})));
			// await new Promise((resolve, reject) => {
			// 	require(modules, resolve, err => {
			// 		console.log(err);
			// 		reject(err);
			// 	});
			// });

			// run
			return new Promise((resolve, reject) => {
				if (!manual) {
					mocha.reporter(PlaywrightReporter);
				}
				mocha.run(failCount => resolve(failCount === 0));
			});
		}

		const modules = new URL(window.location.href).searchParams.getAll('m');
		if (Array.isArray(modules) && modules.length > 0) {
			console.log('MANUALLY running tests', modules);

			loadAndRun(modules, true).then(() => console.log('done'), err => console.log(err));
		}
	</script>
</body>

</html>
